//
//  ViewController.swift
//  myapp
//
//  Created by mac on 2025/6/23.
//

import Cocoa

class ViewController: NSViewController {

    var isRecording = false
    var thread: Thread?
    let btn = NSButton.init(title: "Button", target: nil, action: nil)

    override func viewDidLoad() {
        super.viewDidLoad()

        self.view.setFrameSize(NSSize(width: 320, height: 240))

        btn.title = "开始录制"
        btn.frame = NSRect(
            x: 320 / 2 - 60,
            y: 240 / 2 - 15,
            width: 120,
            height: 30
        )
        btn.bezelStyle = .rounded
        btn.setButtonType(.pushOnPushOff)

        // callback
        btn.target = self
        btn.action = #selector(myFunc)

        self.view.addSubview(btn)
    }

    @objc
    func myFunc() {
        self.isRecording = !self.isRecording

        if isRecording {
            thread = Thread.init(
                target: self,
                selector: #selector(self.startRecording),
                object: nil
            )
            thread?.start()
            self.btn.title = "停止录制"
        } else {
            self.btn.title = "开始录制"
            set_record_status(0)
            print("停止录制 pressed")
        }

    }

    @objc
    func startRecording() {
        print("开始录制")
        record_audio()
    }

    override var representedObject: Any? {
        didSet {
            // Update the view, if already loaded.
        }
    }
}
